package com.nextel.project.canje.rms;

import com.nextel.project.canje.bean.CabCanjeBean;
import com.nextel.project.cliente.bean.ClienteBean;
import pe.com.nextel.rms.GestorRMS;
import pe.com.nextel.rms.Persistente;
import javax.microedition.rms.RecordEnumeration;
import pe.com.nextel.util.ArrayList;
import pe.com.nextel.util.Iterator;
import pe.com.nextel.util.List;

/**
 *
 *  
 */
public class RMSCanje extends GestorRMS {

    public RMSCanje() {
        super("RMS_CANJE");
    }

    protected Persistente createObjectBean() {
        return new CabCanjeBean();
    }

    public void grabarCanje(CabCanjeBean canje) throws Exception {
        try {
            abrirRS();
            guardar(canje);
        } finally {
            cerrarRS();
        }
    }

    /**
     * Para recuperar un pedido
     * @param cliente
     * @return
     * @throws java.lang.Exception
     */
    public List getListaCanjePorCliente(ClienteBean cliente) throws Exception {
        try {
            abrirRS();
            List lista = new ArrayList();
            for (Iterator it = cliente.getLstIdRecordSet().iterator(); it.hasNext();) {
                Integer id = (Integer) it.next();
                if (id.intValue() != 0) {
                    CabCanjeBean Bean = (CabCanjeBean) getRecordObject(id.intValue());
                    Bean.setIdRecodset(id.intValue());
                    lista.add(Bean);
                }
            }
            return lista;
        } finally {
            cerrarRS();
        }
    }

    public List getCanjePendientesEnvio() throws Exception {
        try {
            abrirRS();
            List lista = new ArrayList();
            CabCanjeBean beanTemp = null;
            RecordEnumeration en = enumerateRecords();
            while (en.hasNextElement()) {
                int id = en.nextRecordId();
                beanTemp = (CabCanjeBean) getRecordObject(id);
                beanTemp.setIdRecodset(id);
                lista.add(beanTemp);
            }
            beanTemp = null;
            System.gc();
            return lista;
        } finally {
            cerrarRS();
        }
    }
}
